package edu.hrbu.kwaidu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbu.kwaidu.exception.CustomException;
import edu.hrbu.kwaidu.mapper.SysMenuMapper;
import edu.hrbu.kwaidu.model.entity.SysMenu;
import edu.hrbu.kwaidu.model.entity.SysRoleMenu;
import edu.hrbu.kwaidu.model.entity.SysUser;
import edu.hrbu.kwaidu.model.vo.RouterVo;
import edu.hrbu.kwaidu.service.SysMenuService;
import edu.hrbu.kwaidu.service.SysRoleMenuService;
import edu.hrbu.kwaidu.service.SysUserService;
import edu.hrbu.kwaidu.util.MenuUtil;
import edu.hrbu.kwaidu.util.RouterUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static edu.hrbu.kwaidu.constant.SystemConstant.*;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author xudengyu
 * @since 2024-01-02
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    /**
     * 获取树形菜单列表
     *
     * @return menuList
     */
    @Override
    public List<SysMenu> getMenuList() {
        List<SysMenu> menuList = list();
        return MenuUtil.buildTree(menuList);
    }

    /**
     * add/Edit
     *
     * @param sysMenu 菜单数据
     */
    @Override
    public void addOrEdit(SysMenu sysMenu) {
        // todo 数据校验
        if (BeanUtil.isEmpty(sysMenu)) throw new CustomException(EXCEPTION_PARAM_BLANK);
        boolean f = sysMenu.getId() == null ? save(sysMenu) : updateById(sysMenu);
        if (!f) throw new CustomException(EXCEPTION_UPDATE_FAIL);
    }

    /**
     * 根据主键删除
     *
     * @param id 菜单主键
     */
    @Override
    public void del(Long id) {
        boolean f = removeById(id);
        if (!f) throw new CustomException(EXCEPTION_REMOVE_FAIL);
    }

}
